// 展示每个item的所有图片
import 'package:flutter/material.dart';
import 'package:love_app/providers/attention_provider.dart';
import 'package:love_app/providers/latest_provider.dart';
import 'package:love_app/providers/recommend_provider.dart';
import 'package:love_app/providers/user_center_provider.dart';
import 'package:provider/provider.dart';
import 'package:yin_drag_sacle/yin_drag_sacle.dart';

class CommunityShowImageUI extends StatelessWidget {
  final int itemId; // item对应的id
  final int index;
  final String prividerType;

  CommunityShowImageUI(
      {@required this.itemId,
      @required this.index,
      @required this.prividerType});

  @override
  Widget build(BuildContext context) {
    var provider = _setProvider(context, prividerType);
    // 根据id获取对应的图片集
    PageController _pageController =
        PageController(initialPage: index); // 默认第一张显示
    return Scaffold(
      backgroundColor: Colors.black,
      body: Container(
        child: _pageViwe(_pageController, provider),
      ),
    );
  }

  // 动态设置赋值provider
  _setProvider(BuildContext context, String prividerType) {
    if (prividerType == "recommend") {
      var provider = Provider.of<RecommendProvider>(context, listen: false);
      return provider;
    } else if (prividerType == "latest") {
      var provider = Provider.of<LatestProvider>(context, listen: false);
      return provider;
    }else if (prividerType == "attention") {
      var provider = Provider.of<AttentionProvider>(context, listen: false);
      return provider;
    }else if (prividerType == "userCenter") {
      var provider = Provider.of<UserCenterProvider>(context, listen: false);
      return provider;
    }
  }

  // 显示图片
  Widget _pageViwe(PageController pageController, dynamic provider) {
    List<String> imagesList = provider.showImages(itemId);
    return PageView.builder(
      itemCount: imagesList.length,
      controller: pageController,
      itemBuilder: (BuildContext context, int index) {
        return InkWell(
          onTap: () {
            Navigator.pop(context);
          },
          // 支持缩放
          child: DragScaleContainer(
            child: Image.network(
              imagesList[index],
              fit: BoxFit.fitWidth,
            ),
          ),
        );
      },
    );
  }
}
